package org.cxy.monitor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class MonitorService {
    private static final Logger logger = LoggerFactory.getLogger(MonitorService.class);

    public void beforeExecute(String methodName, Object[] args) {
        logger.info("【Monitor】{} 开始执行，参数: {}", methodName,
                args != null ? java.util.Arrays.toString(args) : "无参数");
    }

    public void afterExecute(String methodName, Object result, long cost, boolean success) {
        String status = success ? "成功" : "失败";
        logger.info("【Monitor】{} 执行{}，耗时: {}ms", methodName, status, cost);
    }

    public void onError(String methodName, Throwable throwable) {
        logger.error("【Monitor】{} 执行异常: {}", methodName, throwable.getMessage());
    }

    public void slowMethod(String methodName, long actualCost, long threshold) {
        logger.warn("【Monitor】{} 执行缓慢，实际耗时: {}ms，超过阈值: {}ms",
                methodName, actualCost, threshold);
    }
}